package com.mswordutil.markdown;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import com.mswordutil.export.Decorator;
import com.mswordutil.export.Poi3Decorator;

/**
 * 文档处理器
 * @author yangyingqiang
 * @time 2015年3月24日 下午6:37:49
 */
public class Processor {

	private static Decorator decorator = new Poi3Decorator();
	
	private static List<Block> readLines(String mdText){
		List<Block> list = new ArrayList<Block>();
		BufferedReader reader = new BufferedReader(new StringReader(mdText));
		String tmp;
		try {
			tmp = reader.readLine();
			while (tmp != null) {
				StringBuilder sb = new StringBuilder();
				Block block = new Block();
				if(tmp.startsWith(MDToken.CODE)){
					tmp = reader.readLine();
					
					boolean isCodeEnd = false;
					while (tmp != null && !isCodeEnd) {
						if(tmp.trim().equals(MDToken.CODE)){
							isCodeEnd = true;
						}else{
							sb.append(tmp+"\n");
							tmp = reader.readLine();
						}
					}
					block.setType(BlockType.CODE);
					block.setValueParts(new ValuePart(sb.toString()));
				}else if(tmp.startsWith(MDToken.HEADLINE)){
					block.setType(BlockType.HEADLINE);
					int i = tmp.lastIndexOf(MDToken.HEADLINE);
					block.setValueParts(new ValuePart(tmp.substring(i+1)));
					block.setLevel(i);
				}else if(tmp.startsWith(MDToken.BLOCKQUOTE)){
					block.setType(BlockType.BLOCKQUOTE);
					block.setValueParts(new ValuePart(tmp.substring(1).trim()));
				}else if(tmp.startsWith(MDToken.IMG)){
					int i = tmp.indexOf(MDToken.IMG);
					int j = tmp.indexOf("]", i);
					boolean isImg = false;
					if(j>0){
						int k = tmp.indexOf("(", j);
						if(k>0&&k==(j+1)){
							int l = tmp.indexOf(")", k);
							if(l>0){
								int m = tmp.indexOf(" ", k);
								int urlEnd = (m>k && m<l)?m:l;
								String url = tmp.substring(k+1, urlEnd);
								block.setType(BlockType.IMG);
								block.setValueParts(new ValuePart(url));
								isImg = true;
							}
						}
					}
					if(!isImg){
						processText(block, tmp);
					}
				}else{
					processText(block, tmp);
				}
				tmp = reader.readLine();
				list.add(block);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	private static void processText(Block block, String content){
		block.setType(BlockType.NONE);
		
		block.setValueParts(new ValuePart(content));
	}
	
	public static void process(String mdText, String filePath) throws FileNotFoundException{
		XWPFDocument doc = new XWPFDocument();
		List<Block> list = readLines(mdText);
		for (Block block : list) {
			XWPFParagraph paragraph = doc.createParagraph();
			decorator.decorate(paragraph, block);
		}
		
		FileOutputStream out = new FileOutputStream(filePath);
		try {
			doc.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		System.out.println("111");
		Processor.process("#大标题\n###小标题 \n```\npublic void main()\n```\n正文，这里是正文哦", "/Users/cevin/Downloads/simple4.docx");
		System.out.println("123123");
	}
}
